﻿using FastCloud.Base;
using FastCloud.ORG.DB;
using FastCloud.ORG.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.ORG
{
    [Route("frame/org/moduleright")]
    public class ModuleRightController : BaseController
    {
        [AutoWired]
        private DB_Module DB_Module;
        [AutoWired]
        private DB_ModuleRight DB_ModuleRight;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/ORG/ModuleRight/ModuleRight_List.cshtml");
        }

        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/ORG/ModuleRight/ModuleRight_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string ModuleName = ValueConvert.ConvertToString(GetFormParam("ModuleName"));
            string ParentModuleGuid = ValueConvert.ConvertToString(GetFormParam("ParentModuleGuid"));
            bool SearchInAll = ValueConvert.ConvertToBoolean(GetFormParam("SearchInAll"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(ModuleName))
            {
                sql += " and ModuleName like @ModuleName";
                ParamList.Add(new ParamEntity() { ParamName = "ModuleName", ParamValue = "%" + ModuleName + "%" });
            }
            if (!SearchInAll)
            {
                if (!string.IsNullOrEmpty(ParentModuleGuid))
                {
                    sql += " and ParentModuleGuid=@ParentModuleGuid";
                    ParamList.Add(new ParamEntity() { ParamName = "ParentModuleGuid", ParamValue = ParentModuleGuid });
                }
            }
            return Result.OK(DB_Module.SelectPage("*", sql, "SortNum desc", ParamList));
        }

        [Route("getMenuRightData"), HttpPost]
        public IActionResult GetMenuRightData()
        {
            Hashtable result = new Hashtable();
            string ModuleGuid = ValueConvert.ConvertToString(GetFormParam("ModuleGuid"));
            List<Frame_ModuleRight> RightList = DB_ModuleRight.SelectAll().Where(a => a.ModuleGuid == ModuleGuid).ToList();
            List<string> OURightList = RightList.Where(a => a.RightType == (int)FastCloud.Base.Enum.ORG.RightType.部门).Select(a => a.RightGuid).ToList();
            List<string> UserRightList = RightList.Where(a => a.RightType == (int)FastCloud.Base.Enum.ORG.RightType.人员).Select(a => a.RightGuid).ToList();
            List<string> RoleRightList = RightList.Where(a => a.RightType == (int)FastCloud.Base.Enum.ORG.RightType.角色).Select(a => a.RightGuid).ToList();
            result.Add("OUGuidList", string.Join(";", OURightList.ToArray()));
            result.Add("UserGuidList", string.Join(";", UserRightList.ToArray()));
            result.Add("RoleGuidList", string.Join(";", RoleRightList.ToArray()));
            return Result.OK(result);
        }


        [Route("updateMenuRightData"), HttpPost]
        public IActionResult UpdateMenuRightData()
        {
            string ModuleGuid = ValueConvert.ConvertToString(GetFormParam("ModuleGuid"));
            string[] OUGuidArray = ValueConvert.ConvertToString(GetFormParam("OUGuidList")).Split(new char[] { ';'},StringSplitOptions.RemoveEmptyEntries);
            string[] UserGuidArray = ValueConvert.ConvertToString(GetFormParam("UserGuidlist")).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            string[] RoleGuidArray = ValueConvert.ConvertToString(GetFormParam("RoleGuidList")).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            DB_ModuleRight.DeleteByModuleGuid(ModuleGuid);
            foreach (string OUGuid in OUGuidArray)
            {
                Frame_ModuleRight Right = new Frame_ModuleRight();
                Right.ModuleGuid = ModuleGuid;
                Right.RightGuid = OUGuid;
                Right.RightType = (int)FastCloud.Base.Enum.ORG.RightType.部门;
                DB_ModuleRight.Insert(Right);
            }
            foreach (string UserGuid in UserGuidArray)
            {
                Frame_ModuleRight Right = new Frame_ModuleRight();
                Right.ModuleGuid = ModuleGuid;
                Right.RightGuid = UserGuid;
                Right.RightType = (int)FastCloud.Base.Enum.ORG.RightType.人员;
                DB_ModuleRight.Insert(Right);
            }
            foreach (string RoleGuid in RoleGuidArray)
            {
                Frame_ModuleRight Right = new Frame_ModuleRight();
                Right.ModuleGuid = ModuleGuid;
                Right.RightGuid = RoleGuid;
                Right.RightType = (int)FastCloud.Base.Enum.ORG.RightType.角色;
                DB_ModuleRight.Insert(Right);
            }
            return Result.OK();
        }

    }
}
